# frozen_string_literal: true

# class for tasks
class Member
  include Mongoid::Document
  include Mongoid::Timestamps
  include Db::Operations
  # paginates_per 10
  field :name, type: String
  field :email, type: String
  field :mobile, type: String
  field :status, type: Integer, default: 0 # 0: draft; 1:planned
  field :t_id, type: String, default: -> { id.to_s }
  field :role, type: String, default: 'admin'
  field :pass_phrase, type: String
  field :deleted, type: Boolean, default: false
  field :is_mock, type: Boolean, default: false

  scope :valid, -> { where(deleted: false) }

  def self.verify(params)
    hmac_secret = ENV['APP_SECRET']
    puts "secret is: #{hmac_secret}"
    jwt_token = JWT.encode(params[:userName], hmac_secret, 'HS256')
    if params[:password] == '1231'
      return { code: 'ok', data: { login_result: 'login_success', token: jwt_token } }.to_json
    end

    { code: 'ok', data: { login_result: 'login_failed', errMsg: '您输入的用户名/密码不正确' } }.to_json
  end
end
